package com.yc.web.servlets;

import com.yc.web.model.JsonModel;
import com.yc.bean.ShoeStyle;
import com.yc.bean.Shoes;
import com.yc.web.ResfoodBiz;
import com.yc.web.model.PageBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@WebServlet(name = "ShoesServlet", value = "/shoes.action")
public class ShoesServlet extends BaseServlet{
    //商品分页查询
    protected void findFoodsByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        JsonModel jm = new JsonModel();
        PageBean pb = null;
        try {
            //取前端的分页的参数
            pb = super.parseObjectFromRequest(req,PageBean.class);
            //调用业务层，完成分页查询...
            ResfoodBiz rb = new ResfoodBiz();
            pb=rb.findByPage(pb);

            //tomcat提供的session =>  redis做缓存
            HttpSession session = req.getSession();
            session.setAttribute("shoeList",pb.getDataset());

            jm.setCode(1);
            jm.setObj(pb);
        } catch (Exception e) {
            e.printStackTrace();
            jm.setCode(0);
            jm.setError("分页查询失败:"+e.getMessage());
        }

        super.writeJson(jm,resp);
    }
    //查询所有的商品
    protected void findAllShoes(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        JsonModel jm = new JsonModel();

        String sql = "SELECT Shoes.sid,sbrand,sgender,ssize,scolor,style,sprice,sdescription,sstockquantity,photourl,ismain FROM Shoes JOIN ShoePhotos ON Shoes.sid = ShoePhotos.sid JOIN ShoeStyle ON Shoes.sstyle=ShoeStyle.styleid WHERE ismain='true'";
        System.out.println("sql语句为："+sql);

        List<Shoes> list = db.select(Shoes.class,sql);

        //Tomcat缓存   =》 redis
        HttpSession session =req.getSession();
        session.setAttribute("shoeList",list);

        jm.setCode(1);
        jm.setObj(list);
        super.writeJson(jm,resp);
    }
    //查询该商品的详情信息
    protected void findShoesInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        JsonModel jm = new JsonModel();
        String sid = req.getParameter("sid");

        String sql = "SELECT Shoes.sid,sbrand,sgender,ssize,scolor,style,sprice,sdescription,sstockquantity,photourl,ismain\n" +
                "FROM Shoes JOIN ShoePhotos ON Shoes.sid = ShoePhotos.sid JOIN ShoeStyle ON Shoes.sstyle=ShoeStyle.styleid\n" +
                "WHERE Shoes.sid=?";
//        System.out.println("sql语句为："+sql);

        List<Shoes> list = db.select(Shoes.class,sql,sid);

        //Tomcat缓存   =》 redis
        HttpSession session =req.getSession();
        session.setAttribute("shoeListInfo",list);

        jm.setCode(1);
        jm.setObj(list);
        super.writeJson(jm,resp);
    }
    //商品类型分类查询(ShoeStyle表)
    protected void findStyle(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        JsonModel jm = new JsonModel();
        String sid = req.getParameter("sid");

        String sql = "SELECT * from ShoeStyle";
//        System.out.println("sql语句为："+sql);

        List<ShoeStyle> list = db.select(ShoeStyle.class,sql);

        //Tomcat缓存   =》 redis
        HttpSession session =req.getSession();
        session.setAttribute("shoeStyleList",list);

        jm.setCode(1);
        jm.setObj(list);
        super.writeJson(jm,resp);
    }
    //显示分类查询的商品
    protected void findStyleShoes(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        JsonModel jm = new JsonModel();
        String styleid = req.getParameter("styleid");

        String sql = "SELECT Shoes.sid,sbrand,sgender,ssize,scolor,style,sprice,sdescription,sstockquantity,photourl,ismain\n" +
                "FROM Shoes JOIN ShoePhotos ON Shoes.sid = ShoePhotos.sid JOIN ShoeStyle ON Shoes.sstyle=ShoeStyle.styleid\n" +
                "WHERE ismain='true' and styleid=?";
//        System.out.println("sql语句为："+sql);

        List<Shoes> list = db.select(Shoes.class,sql,styleid);

        //Tomcat缓存   =》 redis
        HttpSession session =req.getSession();
        session.setAttribute("shoeStyleList",list);

        jm.setCode(1);
        jm.setObj(list);
        super.writeJson(jm,resp);
    }
}
